<?php

require_once __DIR__ . '/../../dao/AnoSemestreDAO.php';
require_once __DIR__ . '/../../dao/SerieDAO.php';
require_once __DIR__ . '/../../dao/ModEnsinoDAO.php';
require_once __DIR__ . '/../../dao/TurnoDAO.php';
require_once __DIR__ . '/../../dao/CREDAO.php';
require_once __DIR__ . '/../../dao/AlunoRegDAO.php';
require_once __DIR__ . '/../../util/DataUtil.php';

/**
 * Classe com algumas abstrações para todos os controladores.
 *
 * @author hyzandey
 */
abstract class AbstractController {

    const VIEWS_FOLDER = '/../../view/';
    const CABECALHO_VIEW = 'cabecalho.phtml';
    const RODAPE = 'rodape.phtml';
    const INSERT_VIEW_INFO_ERRO =
            "É necessário enviar um array do Tipo 'nome => valor'";
    const VIEW_INFO = 'viewInfo';
    const TITULO_VIEW = 'titulo';

    public abstract function executa();
    protected abstract function getTituloView();

    protected function loadView($view) {
        $this->insertViewInfo(array(
            self::TITULO_VIEW => $this->getTituloView()
        ));
        $this->includeView(self::CABECALHO_VIEW);
        $this->includeView($view);
        $this->includeView(self::RODAPE);
    }
    
    protected function insereCabecalhoCSV() {
        header_remove();
        header("Content-type: text/csv");
        header("Content-Disposition: attachment; filename=\"lista_alunos.csv\"");
    }

    public static function includeView($view) {
        include __DIR__ . self::VIEWS_FOLDER . $view;
    }

    protected function insertViewInfo($info) {
        if (!isset($_REQUEST[self::VIEW_INFO])) {
            $_REQUEST[self::VIEW_INFO] = array();
        }

        if (gettype($info) != 'array') {
            throw new
            InvalidArgumentException(self::INSERT_VIEW_INFO_ERRO);
        }

        foreach ($info as $key => $value) {
            $_REQUEST[self::VIEW_INFO][$key] = $value;
        }
    }

    protected function getPost($inputName) {
    	$valor = DataUtil::getValor($_REQUEST[$inputName]);
    	
    	if(gettype($valor) == 'array'){
    		foreach ($valor as $key => $value){
    			if(gettype($value) == 'string'){
    				$valor[$key] = trim($value);
    			}
    		}
    	}elseif (gettype($valor) == 'string'){
    		$valor = trim($valor);
    	}
    	
        return $valor;
    }

    public static function getViewInfo($info) {
        return DataUtil::getValor($_REQUEST[self::VIEW_INFO][$info]);
    }

}

?>
